﻿/********************************************************************************************
 * 文件名称:	Nation.cs
 * 设计人员:	yan(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	管理员
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;

namespace Shanfree.SharpMovie.SQLServerDAL
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class Nation : DBPersistenceBase<NationInfo>, INation
    {

        #region SQL
        private const string SQL_SELECT_ALL = @"SELECT *
                                                                              FROM [dbo].[Nation]";
        private const string SQL_SELECT_BY_ENGLISHNAME = @"SELECT *
                                                                              FROM [dbo].[Nation] WHERE EnglishName = @EnglishName";
        private const string SQL_SELECT_MOVIEID = @"SELECT *
                                                                              FROM [dbo].[V_Nation] WHERE MovieID = @MovieID";
        
        #endregion

        #region 数据库连接串
        /// <summary>
        /// 数据库连接串
        /// </summary>
        protected override string ConnectionString
        {
            get
            {
                return DBConnection.ConnectionString;
            }
        }
        #endregion

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<NationInfo> GetList()
        {
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_ALL);
            IList<NationInfo> list = GetListFromReader(reader);
            int pageSize = 200;
            CustomList<NationInfo> cList = PageAmoutHelper<NationInfo>.GetCutomList(this.ConnectionString, SQL_SELECT_ALL, pageSize, list);
            return cList;
        }

        /// <summary>
        /// 根据英文名称获取国家实体
        /// </summary>
        /// <param name="englishName">国家的英文名称</param>
        /// <returns></returns>
        public NationInfo GetByValue(string englishName)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@EnglishName", englishName);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_BY_ENGLISHNAME,param);
            IList<NationInfo> list = GetListFromReader(reader);
            if (list.Count == 0)
            {
                string otherID = "18";
                return GetByID(otherID);
            }
            return list[0];
        }

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="movieID">电影主键</param>
        /// <returns></returns>
        public IList<NationInfo> GetListByMovieID(string movieID)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MovieID", movieID);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_MOVIEID);
            IList<NationInfo> list = GetListFromReader(reader);
            int pageSize = 100;
            CustomList<NationInfo> cList = PageAmoutHelper<NationInfo>.GetCutomList(this.ConnectionString, SQL_SELECT_MOVIEID, pageSize, list);
            return cList;
        }
    }
}
